// pages/ shoppingCart/ shoppingCart.js

const app = getApp();
let Api = require('../../api/api.js');
Page({
    data: {
        tabbarIndex:1,
        totalAmount: '0',
        showShoppingCart:true,
        shoppingCartList: [], // 购物车列表
        checkedArray:[],
        wcids: '',//购物车标识ID，多个逗号隔开
    },
    onLoad: function (options) {
        let self = this;
        Api.get(app.globalData.mallDomain, {//5）展示购物车中的商品信息
            doShowCarInfo: 1,
            token: app.globalData.token,
        }).then(res => {
            console.log(res,'购物车列表数据')
            let listData = [];
            res.data.list.forEach(ele => {
                listData.push({
                    img: ele.wpCover,
                    title: unescape(ele.wpName),
                    nameplate: ele.wsOneValue,
                    originalPrice: ele.wpOldPrice,
                    actualPrice: ele.wsPrice,
                    val: ele.wcInt,
                    checked:false,
                    wcid: ele.wcid,//购物车标识ID
                })
            })
            self.setData({
                shoppingCartList: listData
            })
        })
    },
    /**
     * 重新加载购物车列表
     */
    loadShoppingCartList(){
        let self = this;
        Api.get(app.globalData.mallDomain, {//5）展示购物车中的商品信息
            doShowCarInfo: 1,
            token: app.globalData.token,
        }).then(res => {
            console.log(res)
            let listData = [];
            res.data.list.forEach(ele => {
                listData.push({
                    img: ele.wpCover,
                    title: unescape(ele.wpName),
                    nameplate: ele.wsOneValue,
                    originalPrice: ele.wpOldPrice,
                    actualPrice: ele.wsPrice,
                    val: ele.wcInt,
                    checked: false,
                    wcid: ele.wcid,//购物车标识ID
                })
            })
            self.setData({
                shoppingCartList: listData
            })
        })
    },
    /**
     * 点击复选框
     */
    checkboxChange: function (e) {
        let self = this;
        self.setData({
            totalAmount: '0', // ☆ 可以优化 
            checkedArray: e.detail.value,
            wcids:'',
        })
        for (let x = 0; x < e.detail.value.length; x++) { //拼接 购物车标识ID
            self.setData({
                wcids: self.data.wcids + ',' + self.data.shoppingCartList[e.detail.value[x]].wcid
            })
        }
        if (self.data.wcids.replace(/(^\s*)|(\s*$)/g, "").substr(0, 1) == ','){ //去除开头字符逗号
            self.setData({
                wcids: self.data.wcids.replace(/(^\s*)|(\s*$)/g, "").substr(1)
            })
        }
        for (let x = 0; x < self.data.shoppingCartList.length; x++) {
            let up = "shoppingCartList[" + x + "].checked";
            self.setData({
                [up]: false
            })
        }
        for (let i = 0; i < e.detail.value.length; i++) {
            let oIndex = e.detail.value[i];
            let up = "shoppingCartList[" + oIndex + "].checked";
            self.setData({
                totalAmount: (parseFloat(self.data.totalAmount) + parseFloat(self.data.shoppingCartList[oIndex].actualPrice) * self.data.shoppingCartList[e.detail.value[i]].val).toFixed(2),
                [up]: true
            })
        }
    },
    /**
     * 点击去结算按钮 进入确认订单界面
     */
    settleAccounts: function(e) {
        let self = this;
        wx.navigateTo({
            url: '../confirmOrder/confirmOrder?sendType=送自己&enterType=购物车进入&wcids=' + self.data.wcids
        })
    },
    /**
     * 添加商品数量
     */
    addVal: function (e) {
        let self = this;
        let oIndex = e.currentTarget.dataset.index;
        let up = "shoppingCartList[" + oIndex + "].val";
        self.setData({
            [up]: parseInt(e.currentTarget.dataset.item.val) + 1
        })
        self.setData({
            totalAmount: '0',
        })
        if (self.data.checkedArray != '') {
            for (let i = 0; i < self.data.checkedArray.length; i++) {
                self.setData({
                    totalAmount: (parseFloat(self.data.totalAmount) + parseFloat(self.data.shoppingCartList[self.data.checkedArray[i]].actualPrice) * self.data.shoppingCartList[self.data.checkedArray[i]].val).toFixed(2),
                })
            }
        }
        Api.get(app.globalData.mallDomain, {//7）修改购物车数量
            doUpCarNum: 1,
            token: app.globalData.token,
            wcid: e.currentTarget.dataset.item.wcid,
            num: parseInt(e.currentTarget.dataset.item.val)  + 1,
        }).then(res => {
            console.log(res, '---------msg==106修改购物车数量成功---------')
        })
    },
    /**
     * 减少商品数量
     */
    reductionVal: function (e) {
        let self = this;
        let oIndex = e.currentTarget.dataset.index;
        let up = "shoppingCartList[" + oIndex + "].val";
        self.setData({
            totalAmount: '0',
        })
        if (e.currentTarget.dataset.item.val != 1) {
            self.setData({
                [up]: e.currentTarget.dataset.item.val - 1
            })
            Api.get(app.globalData.mallDomain, {//7）修改购物车数量
                doUpCarNum: 1,
                token: app.globalData.token,
                wcid:e.currentTarget.dataset.item.wcid,
                num:e.currentTarget.dataset.item.val - 1,
            }).then(res => {
                console.log(res,'---------msg==106修改购物车数量成功---------')
            })

            if (self.data.checkedArray != '') {
                for (let i = 0; i < self.data.checkedArray.length; i++) {
                    self.setData({
                        totalAmount: (parseFloat(self.data.totalAmount) - parseFloat(self.data.shoppingCartList[self.data.checkedArray[i]].actualPrice) * self.data.shoppingCartList[self.data.checkedArray[i]].val).toFixed(2),
                    })
                }
            }
        } else {
            wx.showModal({
                title:'',
                content:'确定要删除收藏吗？',
                cancelColor:'#1672fb',
                confirmColor: '#1672fb',
                success:function(res){
                    if (res.confirm == true){
                        //请求接口删除数据
                        let newArray = self.data.shoppingCartList;
                        newArray.splice(oIndex, 1)
                        self.setData({
                            shoppingCartList: newArray
                        })
                        Api.get(app.globalData.mallDomain, {//6）删除购物车
                            doDelCar: 1,
                            token: app.globalData.token,
                            wcid:e.currentTarget.dataset.item.wcid,
                        }).then(res => {
                            console.log(res,'---------msg==105删除购物车成功---------')
                        })
                    }
                },
            })
            


        }
    },
    /**
     * 点击去逛逛
     */
    toIndex: function (e) {
        let self = this;
        wx.navigateTo({
            url: '../index/index'
        })
    }, 

    
    


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})